package com.example.finalwork2.service.impl;

import com.example.finalwork2.component.VehicleSignalAdapter;
import com.example.finalwork2.dto.WarnRequestItem;
import com.example.finalwork2.dto.WarningInfo;
import com.example.finalwork2.entity.VehicleInfo;
import com.example.finalwork2.entity.VehicleSignal;
import com.example.finalwork2.handler.JudgeHandler;
import com.example.finalwork2.mapper.VehicleInfoMapper;
import com.example.finalwork2.service.WarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WarnServiceImpl implements WarnService {

    @Autowired
    private VehicleSignalAdapter adapter;

    @Autowired
    private JudgeHandler judgeHandler;


    //根据·车架来查电池类型，因为在我的判断逻辑中，电池类型非常重要
    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;




    @Override
    public List<WarningInfo> processWarns(List<WarnRequestItem> requestList) {
        List<WarningInfo> resultList = new ArrayList<>();
        for (WarnRequestItem item : requestList) {
            try {
                VehicleInfo vehicleInfo = vehicleInfoMapper.selectByframeNo(item.getCarId());
                if (vehicleInfo == null) {

                    System.err.println("未找到车辆信息，车ID: " + item.getCarId());
                    continue;
                }

                String batteryType = vehicleInfo.getBatteryType();
                VehicleSignal signal = adapter.adapt(item, batteryType);
                if (signal == null) {
                    System.err.println("空的");
                    continue;
                }
                judgeHandler.handle(signal);
                resultList.addAll(signal.getWarnings());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultList;
    }


}
